package com.moorror.codegenvel.view.window.codegen.view;

import com.intellij.openapi.actionSystem.ActionManager;
import com.intellij.openapi.actionSystem.ActionPlaces;
import com.intellij.openapi.actionSystem.DefaultActionGroup;
import com.intellij.openapi.application.ApplicationManager;
import com.intellij.openapi.editor.Editor;
import com.intellij.openapi.project.Project;
import com.intellij.openapi.vfs.VirtualFile;
import com.moorror.codegenvel.common.notification.CodeGenByVelocityNotifier;
import com.moorror.codegenvel.common.select.MRInputText;
import com.moorror.codegenvel.common.tree.actions.gencode.ExtractVelocityFileChooseAction;
import com.moorror.codegenvel.common.tree.actions.gencode.ExtractVelocityFileContentReplaceAction;
import com.moorror.codegenvel.tab.TabWrapEditor;
import com.moorror.codegenvel.utils.GuiUtils;
import com.moorror.codegenvel.utils.WriteUiUtils;
import com.moorror.codegenvel.view.window.codegen.domain.ExtractFileDesc;
import com.moorror.codegenvel.view.window.codegen.domain.ExtractVariableColumn;
import lombok.Getter;
import org.apache.commons.lang3.StringUtils;

import javax.swing.*;
import java.awt.*;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;

/**
 * @Author jiyanghuang
 * @Date 2025/8/24 16:37
 */
public class JPanelExtractVelocityTemplate extends JPanel {

    protected JPanel toolBarPanel;

    protected JScrollPane treePanel;

    private Project project;

    private Editor editor;

    private ExtractVelocityFileChooseAction fileChooseAction;

    private MRInputText mrInputText;

    @Getter
    private final ExtractFileDesc extractFileDesc;

    private final DialogFormExtractVelocityTemplate dialogParent;

    public JPanelExtractVelocityTemplate(Project project, DialogFormExtractVelocityTemplate dialogParent) {
        this.project = project;
        this.dialogParent = dialogParent;
        toolBarPanel = new JPanel();
        treePanel = new JScrollPane();
        toolBarPanel.setLayout(new BorderLayout());
        setLayout(new BorderLayout());
        add(toolBarPanel, BorderLayout.NORTH);
        add(treePanel, BorderLayout.CENTER);

        editor = TabWrapEditor.createEditorStatic("name", "txt");
        treePanel.setViewportView(editor.getComponent());
        extractFileDesc = new ExtractFileDesc();
        extractFileDesc.setFileContent("");
        extractFileDesc.setEditor(editor);
        WriteUiUtils.writeEditor(this.editor, "");
        installActions();

    }

    protected void installActions() {

        DefaultActionGroup actionGroup = new DefaultActionGroup("ExtractGroup", false);
        if (null != ApplicationManager.getApplication()) {
            fileChooseAction = new ExtractVelocityFileChooseAction(extractFileDesc, this::updateFileChoose);
            actionGroup.add(fileChooseAction);
            actionGroup.add(new ExtractVelocityFileContentReplaceAction(extractFileDesc, this::updateReplace));
            mrInputText = new MRInputText(40, "Template Name Can Not Empty");
            actionGroup.add(mrInputText);
        }

        GuiUtils.installActionGroupInToolBar(actionGroup, toolBarPanel, ActionManager.getInstance(), ActionPlaces.TOOLBAR, true);
    }

    public void fetchFromPanel(){
        extractFileDesc.setFileName(mrInputText.fetchContent());
    }

    public void updateFileChoose(ExtractFileDesc extractFileDesc){
        mrInputText.fillContent(extractFileDesc.getFileName());
    }

    public void updateReplace(ExtractFileDesc extractFileDesc) {
        List<ExtractVariableColumn> values = (List) dialogParent.fetchTableValue();
        Collections.sort(values, new Comparator<ExtractVariableColumn>() {
            @Override
            public int compare(ExtractVariableColumn o1, ExtractVariableColumn o2) {
                String one = o1.getVarReplace();
                if(null == one){
                    one = "";
                }
                String two = o2.getVarReplace();
                if(null == two){
                    two = "";
                }
                return two.length() - one.length();
            }
        });
        String content = extractFileDesc.getFileContent();
        if(StringUtils.isBlank(content)){
            CodeGenByVelocityNotifier.notify("Template Content Can Not Empty");
            return;
        }
        for (ExtractVariableColumn column : values) {
            if(StringUtils.isBlank(column.getVarReplace())){
                continue;
            }
            String varVelocity = "\\$!{" + column.getVarVelocity() + "}";
            content = content.replaceAll(column.getVarReplace(), varVelocity);
            extractFileDesc.addContextKey(column.getVarVelocity(), column.getVarReplace());
        }
        extractFileDesc.setFileContent(content);
        WriteUiUtils.writeEditor(extractFileDesc.getEditor(), content);
    }

    public String templateName(){
        return mrInputText.fetchContent();
    }

    public String templateContent(){
        return extractFileDesc.getFileContent();
    }

    public void updateReplaceFast(Project project, VirtualFile virtualFile){
        extractFileDesc.fileChooser(project, virtualFile);
        mrInputText.fillContent(extractFileDesc.getFileName());

        updateReplaceFast(extractFileDesc);
    }

    public void updateReplaceFast(ExtractFileDesc extractFileDesc){

//        extractFileDesc.fileChooser(project, virtualFile);

        List<ExtractVariableColumn> values = (List) dialogParent.fetchTableValue();
        Collections.sort(values, new Comparator<ExtractVariableColumn>() {
            @Override
            public int compare(ExtractVariableColumn o1, ExtractVariableColumn o2) {
                String one = o1.getVarReplace();
                if(null == one){
                    one = "";
                }
                String two = o2.getVarReplace();
                if(null == two){
                    two = "";
                }
                return two.length() - one.length();
            }
        });
        String content = extractFileDesc.getFileContent();
        if(StringUtils.isBlank(content)){
            CodeGenByVelocityNotifier.notify("Template Content Can Not Empty");
            return;
        }
        for (ExtractVariableColumn column : values) {
            if(StringUtils.isBlank(column.getVarReplace())){
                continue;
            }
            String varVelocity = "\\$!{" + column.getVarVelocity() + "}";
            content = content.replaceAll(column.getVarReplace(), varVelocity);
            extractFileDesc.addContextKey(column.getVarVelocity(), column.getVarReplace());
        }
        extractFileDesc.setFileContent(content);
        WriteUiUtils.writeEditor(extractFileDesc.getEditor(), content);
    }

}
